package com.web.biz.activity.service;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.exception.SwordBaseCheckedException;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.kernel.utils.SwordSequenceUtils;
import com.css.sword.platform.web.adapter.newsword.pagequery.IPageQueryDAO;
import com.css.sword.platform.web.adapter.newsword.utils.PageQueryUtils;
import com.css.sword.platform.web.controller.BaseDomainCtrl;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.css.sword.platform.web.fileupload.intimeup.SwordFileItem;
import com.web.common.dataDictionary.DE.DE_ACTIVITYID;
import com.web.common.dataDictionary.DE.DE_HD_FQSJ;
import com.web.common.dataDictionary.DE.DE_HD_LOGO;
import com.web.common.dataDictionary.DE.DE_HD_SHSJ;
import com.web.common.dataDictionary.DE.DE_HD_SHZT;
import com.web.common.dataDictionary.DE.DE_ID;
import com.web.common.dataDictionary.DE.DE_QRCODE_ID;
import com.web.common.dataDictionary.DE.DE_QRCODE_IMGDOMAIN;
import com.web.common.dataDictionary.DE.DE_QRCODE_OPPTIME;
import com.web.common.dataDictionary.DE.DE_WEBUSERNAME;
import com.web.common.dataDictionary.PO.TabActivityPO;
import com.web.common.dataDictionary.PO.TabQrcodePO;
import com.web.common.utils.DateFormatUtil;
import com.web.common.utils.FileDownloadUtiles;
import com.web.common.utils.FileUploadUtiles;
import com.web.common.utils.LoginInfoUtil;
import com.web.common.utils.ParamConfig;
import com.web.common.utils.StringUtil;

@ServiceContainer
public class ActivityService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(ActivityService.class);

	public ActivityService() throws Exception {
		super();
	}


	/**
	 * 初始化发起活动表单
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "ActivityService_initHdForm")
	public IResData initHdForm(IReqData req) throws Exception {

		log.debug("开始进入 ActivityService_initHdForm 方法。");

		IResData res = new SwordRes();
		String userName = LoginInfoUtil.getLoginUserName();
		HashMap map = new HashMap();
		map.put("webusername", userName);
		res.addForm("form_activity", map);
		
		res.addPage("pages/biz/activity/activityForm.jsp");
		return res;
	}

	 		
	/**
	 * 初始化我的活动列表
	 */
	@SuppressWarnings("rawtypes")
	@Service(serviceName = "ActivityService_initHdList")
	public IResData initHdList(IReqData req) throws Exception {

		log.debug("开始进入 ActivityService_initHdList 方法。");

		IPageQueryDAO pdao=PageQueryUtils.getPageQueryDAO(); 

		IResData res = new SwordRes();
		SQLParam param = new SQLParam();
		param.addParam(LoginInfoUtil.getLoginUserName());
		List list = pdao.pageQueryByKey("grid_activity", "ActivityService_queryListByYhm", param);
		
		res.addTable("grid_activity", list);
		res.addPage("pages/biz/activity/activityList.jsp");
		return res;

	}

	 		
	/**
	 * 提交后保存活动和二维码信息
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "ActivityService_saveHdAndEwm")
	public IResData saveHdAndEwm(IReqData req) throws Exception {

		log.debug("开始进入 ActivityService_saveHdAndEwm 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		dao.getTransactionService().setAutoCommit(false);
		String tjhdflag = "false";
		IResData res = new SwordRes();
		try {
			
			//保存活动模块开始
			TabActivityPO tPo=(TabActivityPO) req.getForm("form_activity",TabActivityPO.class);
			//保存活动logo
			List<SwordFileItem> fjlist = BaseDomainCtrl.class.newInstance().getFormUploadList2(req, "form_activity", "fj");
			String hdlogo = FileUploadUtiles.uploadFileAndGetFileXML(fjlist, FileUploadUtiles.DOMAIN_ACTIVITYLOGO);
			tPo.setId(new DE_ID(SwordSequenceUtils.generateRandomString()));
			tPo.setWebusername(new DE_WEBUSERNAME(LoginInfoUtil.getLoginUserName()));
			tPo.setHdLogo(new DE_HD_LOGO(hdlogo));
			tPo.setHdFqsj(new DE_HD_FQSJ(DateFormatUtil.getNowDate()));
			tPo.setHdShzt(new DE_HD_SHZT("dsh"));
			tPo.setHdShsj(new DE_HD_SHSJ(DateFormatUtil.getNowDate()));
			int i = dao.insertPO(tPo);
			//保存活动模块结束
			
			//保存二维码开始
			TabQrcodePO ePo=(TabQrcodePO) req.getForm("form_activity",TabQrcodePO.class);
			ePo.setQrcodeId(new DE_QRCODE_ID(SwordSequenceUtils.generateRandomString()));
			ePo.setWebusername(new DE_WEBUSERNAME(LoginInfoUtil.getLoginUserName()));
			ePo.setActivityid(new DE_ACTIVITYID(tPo.getId().value));
			ePo.setQrcodeImgdomain(new DE_QRCODE_IMGDOMAIN(ParamConfig.getInstanse().getParam("fileDomainPath")));
			ePo.setQrcodeOpptime(new DE_QRCODE_OPPTIME(DateFormatUtil.getNowDate()));
			int j = dao.insertPO(ePo);
			//保存二维码结束
			
			if (i >0 && j>0) {
				dao.commitAll();
				tjhdflag = "true";
			}
			
		} catch (Exception e) {
			dao.rollbackAll();
			e.printStackTrace();
		}
		
		res.addAttr("tjhdflag", tjhdflag);
		return res;
	}
	
	/**
	 * 打开活动表单页
	 * @param req
	 * @return
	 * @throws SwordBaseCheckedException
	 * @throws UnsupportedEncodingException
	 */
	@Service(serviceName = "ActivityService_openShow")
	public IResData openShow(IReqData req) throws SwordBaseCheckedException, UnsupportedEncodingException {
		log.debug("进入ActivityService_openShow方法");
		
		SwordRes res = new SwordRes();
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		String id = StringUtil.parse2String(req.getAttr("id"));
		SQLParam param = new SQLParam();
		param.addParam(id);
		Map<String, Object> map = dao.queryMapByKey("ActivityService_queryById", param);
		//表单相关附件开始
		String fj = StringUtil.parse2String(map.get("hdLogo"));
		if(StringUtils.isNotEmpty(fj)){
			res.addListWithDataName("hdLogo", FileDownloadUtiles.getfileDownloadMap(fj));
		}
		//表单相关附件结束
		//二维码图片开始
		String webFullPath=ParamConfig.getInstanse().getParam("fileDomainPath")+StringUtil.parse2String(map.get("qrcodeImgpath"));
		res.addAttr("webFullPath", webFullPath);
		res.addJSTL("qrcodePassword", StringUtil.parse2String(map.get("qrcodePassword")));
		//二维码图片结束
		res.addForm("form_activity", map);
		res.addPage("pages/biz/activity/activityDetailForm.jsp");
		return res;
	}
}